<?php
/**
 * @version 	$Id: fFactory.class.php 82 2010-03-17 08:45:39Z mylifeisskidrow@gmail.com $
 * @author 		$Author: mylifeisskidrow@gmail.com $
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 * @license 	GNU/GPL see license.php
 */

defined('IN_FLYWHEEL') or die('Restricted Access');

/**
 * Factory
 * 
 * @package 	system
 *
 */

class fFactory {
	private static $instances;
	
	/**
	 * Create Application
	 *
	 * @param string $name
	 * @param string $evironment
	 * @param string $debug
	 * @param string $type
	 * 
	 * @return fApplication
	 */
	public static function createApplication($name, $evironment, $debug, $type) {
		$class = 'f'.ucfirst($type).'Application';
		require _SYSTEMS_PATH_.'application'._DS_.$class.'.class.php';
		self::$instances['app'] = new $class($name);
		fConfig::set('environment', $evironment);
		fConfig::set('debug', $debug);	
		
		return self::$instances['app'];
	}
	
	/**
	 * Get Application
	 *
	 * @static
	 * @return fApplication
	 */
	public static function getApplication() {
		return self::$instances['app'];
	}
	
	/**
	 * Get Registry
	 * 
	 * @static 
	 * @return fRegistry
	 */
	public static function getRegistry() {		
		if (!isset(self::$instances['registry'])) {
			self::$instances['registry'] = new fRegistry();							
		}
		
		return self::$instances['registry'];
	}
	
	/**
	 * Get Request
	 *
	 * @static 
	 * @return fRequest
	 */
	public static function getRequest() {
		if (!isset(self::$instances['request'])) {
			self::$instances['request'] = new fRequest();							
		}
		
		return self::$instances['request'];
	}
	
	/**
	 * Get Response
	 *
	 * @static 
	 * @return fResponse
	 */
	public static function getResponse() {
		if (!isset(self::$instances['response'])) {
			self::$instances['response'] = new fResponse();
		}
		
		return self::$instances['response'];
	}
	
	/**
	 * Get Router
	 * 
	 * @static 
	 * @return fRouter
	 */
	public static function getRouter() {
		if (!isset(self::$instances['router'])) {
			self::$instances['router'] = new fRouter(); 
		}
		return self::$instances['router'];
	}
	
	/**
	 * Add Controller
	 *
	 * @param instance of fController $controller
	 */
	public static function addController($controller) {
		self::$instances['controller'] = $controller;		
	}
	
	/**
	 * Get Controller
	 *
	 * @return fController
	 */
	public static function getController() {
		return self::$instances['controller'];
	}

	/**
	 * Get Template
	 * 
	 * @static 
	 * @param string $engine. Template Engine.
	 * @return fTemplate
	 */
	public static function getTemplate($engine = null) {		
		$config = fConfig::getConfigByNamespace();		
		if ($engine == null && isset($config['template']['engine'])) {
			$engine = $config['template']['engine'];
			$class = 'f'.ucfirst($config['template']['engine']).'Engine';
		}
		else {			
			$engine = $class = 'fTemplate';
		}
		
		if (!isset(self::$instances['template'][$engine])) {
			require_once _SYSTEMS_PATH_.'templates'._DS_.$class.'.class.php';
			//fLoader::import('templates.' . $class);
			self::$instances['template'][$engine] = new $class; 			
		}
		return self::$instances['template'][$engine];
	}
	
	/**
	 * Get Session
	 * 
	 * @static 
	 * @param array $option. Default = null
	 * 
	 * @return fSession
	 */
	public static function getSession($options = array()) {
		if (sizeof($options) === 0) {
			if (!fConfig::has(null, 'session')) {
				fConfig::load(_APP_PATH_.'config'._DS_.'session.ini', 'session');								
			}
			$option = fConfig::getConfigByNamespace('session');
		}
		
		$key = serialize($options);
		if (!isset(self::$instances['session'][$key])) {
			self::$instances['session'][$key] = new fSession($option);
		}
		return self::$instances['session'][$key];
	}
	
	/**
	 * Get User
	 * 
	 * @static 
	 * @return fUser
	 */
	public static function getUser() {
		if (!isset(self::$instances['user'])) {
			self::$instances['user'] = new fUser();
		}
		return self::$instances['user'];
	}
	
	/**
	 * Get Document
	 * 
	 * @static
	 * @return fHtmlDocument
	 */
	public static function getDocument() {
		if (!isset(self::$instances['document'])) {
			self::$instances['document'] = new fHtmlDocument();
		}
		
		return self::$instances['document'];
	}
	
	/**
	 * Get I18n
	 * 
	 * @static 
	 * @return fI18N
	 */
	public static function getI18N() {
		if (!isset(self::$instances['i18n'])) {
			self::$instances['i18n'] = new fI18N();
		}
		
		return self::$instances['i18n'];
	}
}